package com.hh.test.util;

import java.net.*;
import java.io.*;

/**
 * @author binyangz
 * 
 */
public class HttpUtil {

	public static void main(String[] args) {
		String encode = "GBK";
		System.out
				.println(getResByUrl(
						"http://218.66.46.49:4002/special/jszk/2017/cjcx/jszk_bscj.asp?a=6&b=281&j=3",
						encode));
	}

	public static String getResByUrl(String urlStr) {
		String str = "";
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.setConnectTimeout(15000);
			urlConnection.setReadTimeout(15000);
			BufferedInputStream in = new BufferedInputStream(
					urlConnection.getInputStream());

			byte[] bytes = new byte[in.available()];
			in.read(bytes, 0, bytes.length);
			str = new String(bytes, "UTF-8");
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public static String getResByUrl(String urlStr, String encode) {
		String str = "";
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.setConnectTimeout(15000);
			urlConnection.setReadTimeout(15000);
			try {
				BufferedInputStream in = new BufferedInputStream(
						urlConnection.getInputStream());
				byte[] bcache = new byte[2048];
				int readSize = 0;// 每次读取的字节长度
				ByteArrayOutputStream infoStream = new ByteArrayOutputStream();
				while ((readSize = in.read(bcache)) > 0) {
					infoStream.write(bcache, 0, readSize);
				}
				return infoStream.toString(encode);
			} catch (IOException e1) {
				throw new Exception("输入流读取异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 
	 * @param url
	 * @param message
	 * @param method
	 *            默认为POST
	 * @return
	 */
	public static String invokeUrl(String urlAddress, String message,
			String method) {
		String str = "";
		try {
			if (method == null || "".equals(method)) {
				method = "POST";
			}
			URL url = new URL(urlAddress);
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.setRequestMethod(method);
			urlConnection.setDoOutput(true);
			urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
			urlConnection.setConnectTimeout(15000);
			urlConnection.setReadTimeout(15000);
			urlConnection.setRequestProperty("Content-Length", message.length()
					+ "");
			OutputStream outStream = urlConnection.getOutputStream();
			outStream.write(message.getBytes("UTF-8"));
			outStream.flush();
			outStream.close();
			BufferedInputStream in = new BufferedInputStream(
					urlConnection.getInputStream());
			byte[] bytes = new byte[in.available()];
			in.read(bytes, 0, bytes.length);
			str = new String(bytes, "UTF-8");
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public static byte[] invokeUrl(String urlAddress, String method) {
		String str = "";
		byte[] bytes = null;
		try {
			if (method == null || "".equals(method)) {
				method = "GET";
			}
			URL url = new URL(urlAddress);
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.setRequestMethod(method);
			// urlConnection.setDoOutput(true);
			urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
			urlConnection.setConnectTimeout(15000);
			urlConnection.setReadTimeout(15000);
			// urlConnection.setRequestProperty("Content-Length",
			// message.length() + "");
			// OutputStream outStream = urlConnection.getOutputStream();
			// outStream.write(message.getBytes("UTF-8"));
			// outStream.flush();
			// outStream.close();
			BufferedInputStream in = new BufferedInputStream(
					urlConnection.getInputStream());
			// bytes = new byte[in.available()];
			in.read(bytes, 0, bytes.length);
			// str = new String(bytes, "UTF-8");
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bytes;
	}
}
